package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.HealthAccessMapper;
import com.itheima.pojo.HealthAccess;
import com.itheima.service.HealthAccessService;
import com.itheima.utils.SortUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;


@Service
public class HealthAccessServiceImpl implements HealthAccessService {

    @Autowired
    HealthAccessMapper healthAccessMapper;


    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<HealthAccess> page = healthAccessMapper.findByCondition(queryPageBean.getQueryString());
          return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void addAccess(Map map, int id) {
        Map<String, String> insufficieMap = (Map) map.get("insufficie");
        Map<String, String> mindMap = (Map) map.get("mind");

        String insufficieNum = "";
        String mindNum = "";
        int count1 = 0;
        int count2 = 0;
        Set<String> set = insufficieMap.keySet();
        String[] nums = set.toArray(new String[set.size()]);
        List<String> listNnms = SortUtils.sortByLenthAndValue(nums);
        for (String num : listNnms) {
            String value = insufficieMap.get(num);
            insufficieNum += value;
            insufficieNum += "+";
            count1 += Integer.parseInt(value);
        }

        Set<String> set1 = mindMap.keySet();
        String[] nums1 = set1.toArray(new String[set1.size()]);
        List<String> ListNum1 = SortUtils.sortByLenthAndValue(nums1);
        for (String num : ListNum1) {
            String value = mindMap.get(num);
            mindNum += value;
            mindNum += "+";
            count2 += Integer.parseInt(value);
        }


        HealthAccess healthAccess = new HealthAccess();
        if (count1 > 35 * 0.75) {
            healthAccess.setInsufficie("是");
        } else if (count1 == 0) {
            healthAccess.setInsufficie("未评估");
        } else {
            healthAccess.setInsufficie("否");
        }

        if (count2 > 5 * 19 * 0.75) {
            healthAccess.setMind("不正常");
        } else if (count2 == 0) {
            healthAccess.setMind("未评估");
        } else {
            healthAccess.setMind("正常");
        }

        healthAccess.setInsufficieNum(insufficieNum);
        healthAccess.setAccessDate(new Date());
        healthAccess.setMindNum(mindNum);
        healthAccess.setId(id);


        healthAccessMapper.updateSetInsufficieNUmAndMind(healthAccess);


    }

    @Override
    public Map CheckAccess(int id) {
        Map<String, Object> map = new HashMap<>();
        Map<String, String> insuMap = new HashMap<>();
        Map<String, String> mindMap = new HashMap<>();

        HealthAccess healthAccess = healthAccessMapper.findInsuNumAndMindNumById(id);

        String insufficieNum = healthAccess.getInsufficieNum();
        String mindNum = healthAccess.getMindNum();

        String[] InsuArr = insufficieNum.split("\\+");
        String[] MindArr = mindNum.split("\\+");

        String key = "";
        int i = 1;
        for (String value : InsuArr) {
            key = "num" + i;
            insuMap.put(key, value);
            i++;
        }

        int x = 1;
        for (String value : MindArr) {
            key = "num" + x;
            mindMap.put(key, value);
            x++;
        }

        map.put("insufficie", insuMap);
        map.put("mind", mindMap);

        return map;
    }

    @Override
    public void del(int id) {
        healthAccessMapper.del(id);
    }

}
